package net.mihome.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import net.mihome.app.R;
import net.mihome.app.ui.GoodsMainActivity;
import net.mihome.app.ui.MiFuwuWuyeActivity;
import net.mihome.app.ui.MifuwuDianpuMainActivity;

public class MifuwuAdapter extends BaseAdapter {
	private Context context;

    public static final int TYPE_OF_SHOP_JINGXUANDIANPU=0;
    public static final int TYPE_OF_SHOP_MAMIBAOBEI=-1;
    public static final int TYPE_OF_SHOP_MEIRONG=-2;
    public static final int TYPE_OF_SHOP_SHENGHUOPEISONG=-3;
    public static final int TYPE_OF_SHOP_XIAOQUCHAOSHI=-4;
    public static final int TYPE_OF_SHOP_JIAZHENGFUWU=3;
	public MifuwuAdapter(Context context) {
		this.context = context;
	}

	private Integer[] images = {
	// 九宫格图片的设置
	        R.drawable.xiaoquwuye_icon,
//            R.drawable.banzheng_icon,
            R.drawable.shangpu_icon,
            R.drawable.xiaoquchaoshi_icon,
            R.drawable.jiazhengfuwu_icon,
//            R.drawable.shenghuopeisong_icon
    };

	private String[] texts = {
	// 九宫格图片下方文字的设置
	        "小区物业",
//            "办证帮助",
            "周边商铺",
            "小区超市",
            "家政服务",
//            "生活配送"
//            "家政服务",
//            "生活配送",
//            "美容养生",
//            "妈咪宝贝"
    };

	// get the number
	@Override
	public int getCount() {
		return images.length;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	// get the current selector's id number
	@Override
	public long getItemId(int position) {
		return position;
	}

	// create view method
	@Override
	public View getView(int position, View view, ViewGroup viewgroup) {
		ImgTextWrapper wrapper;
		if (view == null) {
			wrapper = new ImgTextWrapper();
			LayoutInflater inflater = LayoutInflater.from(context);
			view = inflater.inflate(R.layout.mifuwu_main_item, null);
			view.setTag(wrapper);
			view.setPadding(15, 15, 15, 15); // 每格的间距
		} else {
			wrapper = (ImgTextWrapper) view.getTag();
		}

		wrapper.imageView = (ImageView) view
				.findViewById(R.id.MainActivityImage);
		wrapper.imageView.setBackgroundResource(images[position]);
		wrapper.textView = (TextView) view.findViewById(R.id.MainActivityText);
		wrapper.textView.setText(texts[position]);
		setItemClick(view,position);
		return view;
	}
	/**
	 * 设置item事件
	 * */
	public void setItemClick(View view,final int postion){
		view.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent=null;
				switch (postion) {
				case 0:
					intent=new Intent(context,MiFuwuWuyeActivity.class);
                    //报修
					context.startActivity(intent);
					break;
				case 1:
					intent=new Intent(context,MifuwuDianpuMainActivity.class);
                    intent.putExtra("title","周边店铺");
                    intent.putExtra("type",MifuwuDianpuMainActivity.SHOP_SHOW_TYPE_SHANGPU);
					context.startActivity(intent);
					break;
                case 2:
                    intent=new Intent(context,GoodsMainActivity.class);
                    intent.putExtra("title","小区超市");
                    intent.putExtra("type",TYPE_OF_SHOP_XIAOQUCHAOSHI);
                    context.startActivity(intent);
                    break;
                case 3:
                    intent=new Intent(context,MifuwuDianpuMainActivity.class);
                    intent.putExtra("title","家政服务");
                    intent.putExtra("type",MifuwuDianpuMainActivity.SHOP_SHOW_TYPE_JIAZHENG);
                    context.startActivity(intent);
                    break;
				default:
					Toast.makeText(context,"无效的链接",Toast.LENGTH_SHORT).show();
					break;
				}
			}
		});
	}
}

class ImgTextWrapper {
	ImageView imageView;
	TextView textView;
}
